package com.mcc.myapplication;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.ComponentActivity;
import androidx.lifecycle.ViewModelProvider;

import com.mcc.myapplication.hello.HelloActivity;
import com.mcc.myapplication.layout.relative.MyFragmentActivity;
import com.mcc.myapplication.layout.relative.RelativeActivity;
import com.mcc.myapplication.model.MainViewModel;

public class MainActivity extends ComponentActivity {

    private TextView mShowCount = null;
    //    private int mCount = 0;
    private static final String TAG = MainActivity.class.getName();

    private MainViewModel viewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_main);
//        System.out.println(R.string.goHello);
        viewModel = new ViewModelProvider(this).get(MainViewModel.class);
        Log.i(TAG, "onCreate()");

        Button plusButton = findViewById(R.id.button_count);
        plusButton.setOnClickListener(v -> plus(v));
        Button subButton = findViewById(R.id.button_toast);
        subButton.setOnClickListener(v -> sub(v));


        Button goHelloButton = findViewById(R.id.goHello);
        goHelloButton.setOnClickListener(v -> startOther(v));

        Button shareActivityButton = findViewById(R.id.shareActivity);
        shareActivityButton.setOnClickListener(v -> shareActivity(v));

        Button nanjingActivity = findViewById(R.id.nanjingActivity);
        nanjingActivity.setOnClickListener(v -> shareNanjActivity(v));

        ImageButton imageButton = findViewById(R.id.pictureBtn);
        imageButton.setOnClickListener(v -> {
            Intent intent = new Intent(this, MyFragmentActivity.class);
            startActivity(intent);
        });

        TextView v = findViewById(R.id.version);
        v.setText("1.0.13");
        refreshNum(MainViewModel.counter);
    }

    void refreshNum(int num) {
        TextView s = findViewById(R.id.show_count);
        s.setText(Integer.toString(num));
    }

    public void sub(View view) {
        refreshNum(--MainViewModel.counter);
    }

    public void plus(View view) {
        refreshNum(++MainViewModel.counter);
//        Toast.makeText(MainActivity.this, "操作", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onStart() {
        super.onStart();

        // The activity is about to become visible.
        Log.i(TAG, "onStart()");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i(TAG, "onResume()");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i(TAG, "onPause()");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i(TAG, "onStop()");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy()");
    }


    public void startOther(View view) {
        Intent messageIntent = new Intent(this, HelloActivity.class);
        startActivity(messageIntent);

//        Intent sendIntent = new Intent(Intent.ACTION_VIEW);
//        sendIntent.setAction(Intent.ACTION_VIEW);
    }

    public void shareActivity(View view) {
        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, "你好,隐式 Intent ");
        sendIntent.setType("text/plain");
        if (sendIntent.resolveActivity(getPackageManager()) != null) {
            startActivity(sendIntent);
        }
    }

    public void shareNanjActivity(View view) {
        Uri webpage = Uri.parse("https://morningcat2018.github.io/2023/11/24/%E6%97%85%E6%B8%B8-%E5%8D%97%E4%BA%AC%E6%97%85%E6%B8%B8%E6%8C%87%E5%8D%97/");
        Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);
        if (webIntent.resolveActivity(getPackageManager()) != null) {
            startActivity(webIntent);
        }
    }
}
